#include "../StdInc.h"
#include "../RealTime/MsgBuffer.h"
#include "../RealTime/MsgManager.h"

namespace GSTrader
{
	namespace TradeStrategy
	{
		CSellSideStrategy::CSellSideStrategy(CStrategySession* pStrategySession, string strStrategyName) : CStrategy(pStrategySession, strStrategyName),
			CConnectionProvider(ptNone), CMarketDataProvider(ptSellSideProvider), CTradeProvider(ptSellSideProvider)
		{
			m_nProviderType = pvtSpread;
			m_pConnectionInfo = new CConnectionInfo(ptSellSideProvider);
			Connect(m_pConnectionInfo);
		}

		CSellSideStrategy::~CSellSideStrategy()
		{
			_delete(m_pConnectionInfo)
		}

		int CSellSideStrategy::GetMaxHand(int nInstrumentId, bool bLong)
		{
			return 0;
		}

		int CSellSideStrategy::Connect(CConnectionInfo* pUser)
		{
			CTradeProvider::m_nConnectStatus = connected;
			CMarketDataProvider::m_nConnectStatus = connected;
			return 0;
		}

		void CSellSideStrategy::DisConnect()
		{
			CTradeProvider::m_nConnectStatus = disconnected;
			CMarketDataProvider::m_nConnectStatus = disconnected;
		}

		void CSellSideStrategy::Subscribe(CInstrument* pInstrument)
		{

		}

		void CSellSideStrategy::OnSubscribe(CInstrument* pInstrument)
		{

		}

		void CSellSideStrategy::Subscribe(CInstrumentLongCode& code)
		{
			assert(false);
		}

		void CSellSideStrategy::Subscribe(CSubscribeInstrument* pSubscript)
		{
			assert(false);
		}

		void CSellSideStrategy::UnSubscribe(string code)
		{

		}

		void CSellSideStrategy::UnSubscribe(vector<string>& vUnSubscript)
		{

		}

		bool CSellSideStrategy::Request(int nIndex, char* pszParam)
		{
			return false;
		}

		CTradeProvider* CSellSideStrategy::GetTradeProvider()
		{
			CTradeProvider* pTradeProvider = _GetTradeProvider();
			if (pTradeProvider && pTradeProvider->IsConnected())
			{
				if (pTradeProvider->m_nPlatformType != ptSellSideProvider)
					return pTradeProvider;
			}

			return NULL;
		}

		CMarketDataProvider* CSellSideStrategy::GetMarketDataProvider()
		{
			CMarketDataProvider* pMarketDataProvider = _GetMarketDataProvider();
			if (pMarketDataProvider && pMarketDataProvider->IsConnected())
			{
				if (pMarketDataProvider->m_nPlatformType != ptSellSideProvider)
					return pMarketDataProvider;
			}

			return NULL;
		}

		bool CSellSideStrategy::GetTradingAccount(CTradingAccount& account)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetTradingAccount(account);
		}

		bool CSellSideStrategy::GetPosition(vector<CPosition>& positions)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetPosition(positions);
		}

		bool CSellSideStrategy::GetPosition(int nInstrumentId, int nLongShort, vector<CPosition>& positions)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetPosition(nInstrumentId, nLongShort, positions);
		}

		bool CSellSideStrategy::GetPositionDetail(vector<CPositionDetail>& vDetail)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetPositionDetail(vDetail);
		}

		bool CSellSideStrategy::GetPositionDetail(int nInstrumentId, vector<CPositionDetail>& vDetail)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetPositionDetail(nInstrumentId, vDetail);
		}

		bool CSellSideStrategy::GetOrder(int nInstrument, int nStatus, vector<COrder>& vOrders)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetOrder(nInstrument, nStatus, vOrders);
		}

		bool CSellSideStrategy::GetTrade(int nInstrument, vector<CTrade>& vTrade)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetTrade(nInstrument, vTrade);
		}

		bool CSellSideStrategy::GetNoTradeOrder(int nInstrumentId, vector<COrder>& vOrders)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetNoTradeOrder(nInstrumentId, vOrders);
		}

		bool CSellSideStrategy::GetExchange(vector<CExchange>& vExchange)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetExchange(vExchange);
		}

		bool CSellSideStrategy::GetAccountInstrument(string strExchangeId, vector<CAccountInstrument*>& vInstrument)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetAccountInstrument(strExchangeId, vInstrument);
		}

		bool CSellSideStrategy::GetAllAccountInstrument(vector<CInstrumentLongCode>& vInstrumentCode)
		{
			CTradeProvider* pTradeProvder = GetTradeProvider();
			if (!pTradeProvder)
				return false;
			else
				return pTradeProvder->GetAllAccountInstrument(vInstrumentCode);
		}

		bool CSellSideStrategy::Replace(COrder* pSrcOrder, COrder* pNewOrder, ReplaceOrderMethod* pMethod)
		{
			return false;
		}

		bool CSellSideStrategy::Send(CExecutionCommond* pExecutionCommond)
		{
			switch (pExecutionCommond->m_nCommondType)
			{
			case cmdSend:
				{
					return OnSendCommond(pExecutionCommond);
				}
				break;
			case cmdCancel:
				{
					return OnCancelCommond(pExecutionCommond);
				}
				break;
			case cmdReplace:
				{
					return OnReplaceCommond(pExecutionCommond);
				}
				break;
			}

			return false;
		}


		void CSellSideStrategy::EmitNewMarketData(CMsg* pMsg)
		{
			m_pFramework->m_pMsgBuffer->OnMsg(pMsg);
		}

		void CSellSideStrategy::EmitExecutionReport(CMsg* pMsg)
		{
			m_pFramework->m_pMsgBuffer->OnMsg(pMsg);
		}

		bool CSellSideStrategy::OnSendCommond(CExecutionCommond* pExecutionCommond)
		{
			return false;
		}

		bool CSellSideStrategy::OnCancelCommond(CExecutionCommond* pExecutionCommond)
		{
			return false;
		}

		bool CSellSideStrategy::OnReplaceCommond(CExecutionCommond* pExecutionCommond)
		{
			return false;
		}

	}
}